<?php
/**
 * Created by PhpStorm.
 * User: hedonglin
 * Date: 2017/10/24
 * Time: 10:59
 */

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use app\admin\model\Normtarget as NormtargetModel;


class Normtarget extends Controller
{
    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'oes_normtarget';
    public $table1 = 'oes_etype';

    /**
     *评教指标页面视图
     * @return mixed
     */
    public function index()
    {

        return $this->fetch();
    }

    /**
     * 级联查询评教指标
     */
    public function getList()
    {
        /*$result = DB::table(array($this->table => 'ntarget', $this->table1 => 'etype',$this->table => 'parent'))
            ->field('ntarget.*,etype.name as ename,parent.name as pname')
            ->where('ntarget.etype_id=etype.id')
            ->order("ntarget.sort_code")
            ->select();*/
        $query = "SELECT tar.*,oe.`name` ename,parent.`name` pname FROM oes_normtarget tar
                    LEFT JOIN oes_etype oe ON oe.id = tar.etype_id
                    LEFT JOIN oes_normtarget parent ON parent.id = tar.parentnormtarget_id";
        $result = DB::query($query);
        return json($result);
    }

    /**
     * 根据id查询指标信息
     * @param $id
     * @return \think\response\Json
     */
    public function getTargetById($id)
    {
        $result = NormtargetModel::get($id);
        $query = "SELECT tar.*,parent.`name` as pname FROM `oes_normtarget` tar
                    LEFT JOIN oes_normtarget parent ON tar.parentnormtarget_id = parent.id
                    WHERE tar.id = ?";
        $result = DB::query($query, [$id]);
        return json($result);
    }

    /**
     * 新增指标
     * @param Request $request
     * @return \think\response\Json
     */
    public function addNormTarget(Request $request)
    {
        // 获取参数仅包含数组中字段
        $data = $request->only(['name', 'entropy', 'sort_code', 'etype_id', 'parentnormtarget_id']);
        if ($data['parentnormtarget_id'] == null) {
            $data = $request->only(['name', 'entropy', 'sort_code', 'etype_id']);
        }
        // 添加数据
        $result = NormtargetModel::create($data);
        // 返回数据
        if ($result) {
            return json(['success' => true, "msg" => '添加成功']);
        } else {
            return json(['success' => false, "msg" => '添加失败']);
        }
    }

    /**
     * 修改指标
     * @param Request $request
     * @return \think\response\Json
     */
    public function updateNormTarget(Request $request)
    {
        // 获取参数仅包含数组中字段
        $data = $request->only(["id", 'name', 'entropy', 'sort_code', 'etype_id', 'parentnormtarget_id']);
        // 添加数据
        $result = NormtargetModel::update($data);
        // 返回数据
        if ($result) {
            return json(['success' => true, "msg" => '修改成功']);
        } else {
            return json(['success' => false, "msg" => '修改失败']);
        }
    }

    /**
     * 级联删除指标
     * @param $id
     * @return \think\response\Json
     */
    public function deleteNormTarget($id)
    {
        // 添加数据
        $result = NormtargetModel::destroy($id);
        // 返回数据
        if ($result) {
            return json(['success' => true, "msg" => '删除成功']);
        } else {
            return json(['success' => false, "msg" => '删除失败']);
        }
    }


}